//
//  API_Message.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/4.
//

import UIKit
import Moya

enum API_Message {
    /// 消息列表
    case messageList(Int)
}

let message_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Message>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_message = MoyaProvider<API_Message>(requestClosure: message_timeoutClosure)

extension API_Message: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .messageList(_):
            return "/User/getMessage"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .messageList(let page):
            ps["page"] = page
            ps["num"] = 10
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}
